package com.uxwine.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

public class ActionWindow extends PopupWindow implements
		PopupWindow.OnDismissListener
{

	private int mnExitCode;
	private Context mCtx;
	private OnWindowEventListener mOnWindowEventListener;

	public static final int CANCEL = -1;
	public static final int OK = 0;

	public ActionWindow(Context ctx)
	{
		super(ctx);
		mCtx = ctx;
		mnExitCode = CANCEL;
		initView();
	}

	public ActionWindow(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		mCtx = context;
		mnExitCode = CANCEL;
		initView();
    }
	
	private void initView()
	{
		this.setFocusable(true);
		setOnDismissListener(this);
		setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
		setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
				WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
	}

	protected Context getContext()
	{
		return mCtx;
	}

	public void setOnWindowEventListener(OnWindowEventListener l)
	{
		mOnWindowEventListener = l;
	}

	public void setContentView(int resId)
	{
		View v = LayoutInflater.from(mCtx).inflate(resId, null);
		setContentView(v);
	}

	public View findViewById(int id)
	{
		return getContentView().findViewById(id);
	}

	public void exit(int code)
	{
		mnExitCode = code;
		dismiss();
	}

	public static interface OnWindowEventListener
	{
		public void onExit(int code);
	}

	@Override
	public void onDismiss()
	{
		if (null != mOnWindowEventListener)
			mOnWindowEventListener.onExit(mnExitCode);
		mnExitCode = CANCEL;
	}
}
